import axios from 'axios';
import ElementUI from 'element-ui';
console.log(process.env.VUE_APP_BASE_API);
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        config.headers.accessToken = localStorage.getItem("accessToken")
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        if (response.data.code == 250004  || response.data.code ==250005) {
            localStorage.clear()
            console.log(window.location);
            location.reload();
        }
        if (response.data.code === 0) {
            return response.data;
        } else {
            if (!response.data.code) {
                return response.data;
            }
            ElementUI.Message({
                message: response.data.msg,
                type: 'error'
            });
            return Promise.reject(response);
        }
    },
    error => {
        return Promise.reject(error);
    }
);

export default service;
